(function () {
	// 所有交互接口定义在此处
    window.api = [
        "event.showSuccessDlg"
    ];


    window.app = {
        isApp: function () {// 
            var ua = navigator.userAgent.toLowerCase();
            if (ua.indexOf('__app') != -1) {
                return true;
            }
            return false;
        }
    }


    var isJSONObject = function (data) {
        return typeof(data) == "object" &&
            Object.prototype.toString.call(data).toLowerCase() == "[object object]" && !data.length;
    }
    var jsonAddJson = function (srcJson, jsonData) {
        for (var p in jsonData) {
            srcJson[p] = jsonData[p];
        }
    }
    var registerAPI = function (obj, names, fun) {
        var n = names.replace(/\..*/, '');
        obj = obj || window.app;
        obj[n] = obj[n] || {};
        n == names ? obj[n] = fun : registerAPI(obj[n], names.replace(n + '.', ''), fun);
    };
    var apiCallback = function (func) {
        if (!func)
            return '';
        var apiCB = 'apiCB' + Math.ceil(Math.random() * 1000000000000);
        window[apiCB] = function (param) {
            func && func(param);
            delete window[apiCB];
        };
        return apiCB;
    };
    for (var i = 0; i < api.length; i++) {
        (function (apiName) {
            registerAPI(null, apiName, function () {
                var argumentArr = Array.prototype.slice.call(arguments);
                var param = {};
                var callbacks = [];
                for (var i = 0; i < argumentArr.length; i++) {
                    if (isJSONObject(argumentArr[i])) {
                        jsonAddJson(param, argumentArr[i]);
                    }
                    if (typeof argumentArr[i] == 'function') {
                        callbacks.push(apiCallback(argumentArr[i]));
                    }
                }
                var param = {
                    'apiName': apiName,
                    'param': param,
                    'callbacks': callbacks.join(",") || ''
                };
                if (app.isApp()){
                    return prompt(JSON.stringify(param));
                }
                console.info(param)
                return ""
            });
        })(api[i])
    }

})();